package com.example.test.dialogfactory;

import android.Manifest.permission;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.activity.result.ActivityResultCallback;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.util.Pair;
import easier.app.x.result.Permission;
import easier.log.logger.Logger;
import easier.webrtc.WebRtcManager;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.webrtc.Camera1Enumerator;
import org.webrtc.CameraEnumerationAndroid.CaptureFormat;
import org.webrtc.CameraEnumerator;
import org.webrtc.SurfaceViewRenderer;
import org.webrtc.VideoCapturer;

public class MainActivity84 extends AppCompatActivity {

    public static void start( Context context ) {

        Intent starter = new Intent( context, MainActivity84.class );
        starter.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP );
        context.startActivity( starter );
    }

    @Override
    protected void onCreate( Bundle savedInstanceState ) {

        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main84 );

        Permission.registerForPermissionsResult( getSupportFragmentManager(), new String[]{permission.CAMERA,
            permission.RECORD_AUDIO}, new ActivityResultCallback< Map< String, Boolean > >() {
            @Override
            public void onActivityResult( Map< String, Boolean > result ) {

                Set< String > strings = result.keySet();
                for ( String string : strings ) {
                    Boolean aBoolean = result.get( string );
                    Logger.i( string + " " + aBoolean );
                }

                SurfaceViewRenderer local = findViewById( R.id.localView );
                local.setMirror( true );
                SurfaceViewRenderer back = findViewById( R.id.backView );
                back.setMirror( false );

                WebRtcManager manager = new WebRtcManager() {
                    @Override
                    public Pair< String, CaptureFormat > configVideoCapturer( CameraEnumerator cameraEnumerator,
                        String[] deviceNames ) {

                        /* 前置摄像头,分辨率小于等于480*640*30 */
                        for ( String deviceName : deviceNames ) {
                            if ( cameraEnumerator.isFrontFacing( deviceName ) ) {
                                List< CaptureFormat > formats = cameraEnumerator.getSupportedFormats( deviceName );
                                for ( CaptureFormat format : formats ) {
                                    if ( format.width <= 480 ) {
                                        return new Pair<>( deviceName, format );
                                    }
                                }
                                return new Pair<>( deviceName, formats.get( 0 ) );
                            }
                        }

                        /* 没有前置的摄像头,使用后置的 */
                        for ( String deviceName : deviceNames ) {
                            if ( !cameraEnumerator.isFrontFacing( deviceName ) ) {
                                List< CaptureFormat > formats = cameraEnumerator.getSupportedFormats( deviceName );
                                return new Pair<>( deviceName, formats.get( 0 ) );
                            }
                        }

                        return null;
                    }
                };
                manager.createMedia( MainActivity84.this, local );

                WebRtcManager backManager = new WebRtcManager() {
                    @Override
                    public String configAudioId() {

                        return "201";
                    }

                    @Override
                    public String configVideoId() {

                        return "202";
                    }

                    @Override
                    public String configSurfaceThreadName() {

                        return "renderThread-200";
                    }

                    @Override
                    public Pair< String, CaptureFormat > configVideoCapturer( CameraEnumerator cameraEnumerator,
                        String[] deviceNames ) {

                        /* 前置摄像头,分辨率小于等于480*640*30 */
                        for ( String deviceName : deviceNames ) {
                            if ( cameraEnumerator.isBackFacing( deviceName ) ) {
                                List< CaptureFormat > formats = cameraEnumerator.getSupportedFormats( deviceName );
                                for ( CaptureFormat format : formats ) {
                                    if ( format.width <= 480 ) {
                                        return new Pair<>( deviceName, format );
                                    }
                                }
                                return new Pair<>( deviceName, formats.get( 0 ) );
                            }
                        }

                        /* 没有前置的摄像头,使用后置的 */
                        for ( String deviceName : deviceNames ) {
                            if ( !cameraEnumerator.isBackFacing( deviceName ) ) {
                                List< CaptureFormat > formats = cameraEnumerator.getSupportedFormats( deviceName );
                                return new Pair<>( deviceName, formats.get( 0 ) );
                            }
                        }

                        return null;
                    }
                };
                backManager.createMedia( MainActivity84.this, back );
            }
        } );
    }

    public static VideoCapturer createCameraCapture( boolean front ) {

        Camera1Enumerator enumerator = new Camera1Enumerator( false );
        final String[] deviceNames = enumerator.getDeviceNames();

        for ( String deviceName : deviceNames ) {
            List< CaptureFormat > supportedFormats = enumerator.getSupportedFormats( deviceName );
            for ( CaptureFormat format : supportedFormats ) {
                Logger.i( deviceName + " " + format.toString() );
            }
        }

        // First, try to find front facing camera
        for ( String deviceName : deviceNames ) {
            if ( front ? enumerator.isFrontFacing( deviceName ) : enumerator.isBackFacing( deviceName ) ) {
                VideoCapturer videoCapturer = enumerator.createCapturer( deviceName, null );

                if ( videoCapturer != null ) {
                    return videoCapturer;
                }
            }
        }

        // Front facing camera not found, try something else
        for ( String deviceName : deviceNames ) {
            if ( front ? !enumerator.isFrontFacing( deviceName ) : !enumerator.isBackFacing( deviceName ) ) {
                VideoCapturer videoCapturer = enumerator.createCapturer( deviceName, null );

                if ( videoCapturer != null ) {
                    return videoCapturer;
                }
            }
        }

        return null;
    }
}